<?php

namespace App\Http\Controllers;

use App\Library\Logic\ActivityZoneLogic;
use App\Library\Logic\ShopCategoryLogic;
use App\Library\Logic\ShopProductLogic;
use App\Library\Requests\Auction\AuctionBidPageRequest;
use App\Library\Requests\Auction\AuctionIndexPageRequest;
use App\Library\Requests\Base\IdRequest;
use App\Library\Requests\Base\SerialNumberRequest;
use App\Library\Requests\Product\ProductUserPageRequest;
use App\Library\Service\AuctionService;
use App\Library\Service\IndexService;
use App\Library\Service\ShopProductService;
use App\Library\Service\SlideService;
use App\Models\SystemConfigModel;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class IndexController extends BaseController
{
    /**
     * @param IndexService $service
     */
    public function __construct(IndexService $service)
    {
        $this->service = $service;
    }

    /**
     * module
     * @return JsonResponse
     */
    public function module(): JsonResponse
    {
        return $this->service->module();
    }

    /**
     * active
     * @return JsonResponse
     */
    public function config(): JsonResponse
    {
        $config = SystemConfigModel::query()->where('id', 1)->value('content');
        return success($config);
    }

    /**
     * auctionPaging
     * @param AuctionIndexPageRequest $request
     * @return JsonResponse
     */
    public function auctionPaging(AuctionIndexPageRequest $request): JsonResponse
    {
        return AuctionService::indexPaging($request);
    }

    /**
     * 出价记录
     * @param AuctionBidPageRequest $request
     * @return JsonResponse
     */
    public function bidPaging(AuctionBidPageRequest $request): JsonResponse
    {
        return AuctionService::bidPaging($request);
    }

    /**
     * indexDetail
     * @param SerialNumberRequest $request
     * @return JsonResponse
     */
    public function auctionDetail(SerialNumberRequest $request): JsonResponse
    {
        return AuctionService::indexDetail($request);
    }

    /**
     * auctionCertification
     * @return JsonResponse
     */
    public function auctionCertification(): JsonResponse
    {
        return AuctionService::certification();
    }

    /**
     * slide
     * @return JsonResponse
     */
    public function slide(): JsonResponse
    {
        return SlideService::list();
    }

    /**
     * auctionNum
     * @param SerialNumberRequest $request
     * @return JsonResponse
     */
    public function auctionNum(SerialNumberRequest $request): JsonResponse
    {
        $k = 'bid:' . $request->serialNumber;
        $str = cache()->has($k) ? cache($k) : '0';
        return success(aesEncrypt($str));
    }

    /**
     * category
     * @return JsonResponse
     */
    public function category(): JsonResponse
    {
        return success(DB::table('shop_category')->where('category_status', 1)->pluck('category_name', 'category_id'));
    }

    /**
     * category
     * @return JsonResponse
     */
    public function goodsList(ProductUserPageRequest $request): JsonResponse
    {

        return success(ShopProductService::commonGoodsPage(ShopProductLogic::goodList($request)));
    }

    /**
     * category
     * @return JsonResponse
     */
    public function newCategory(): JsonResponse
    {
        return success(DB::table('shop_category')
            ->where('category_status', 1)->whereIn('category_parent_id', ['1', '2', '23'])->get());
    }

    /**
     * category
     * @return JsonResponse
     */
    public function allCategory(): JsonResponse
    {
        $parm = ['category_id',
            'category_status',
            'category_name',
            'category_parent_id',
            'category_top_parent_id',
            'category_level',
            'sort',
            'class_jump_url'
        ];
        $oneList = ShopCategoryLogic::query()
            ->where([
                'category_status' => 1,
                'status_delete' => 0,
                ['category_level', '=', null]
            ])->get($parm);
        if (!empty($oneList)) {
            $oneList = $oneList->toArray();
            foreach ($oneList as &$oVal) {
                $twoList = ShopCategoryLogic::query()
                    ->where([
                        'category_status' => 1,
                        'status_delete' => 0,
                        ['category_level', '!=', null],
                        'category_parent_id' => $oVal['category_id']
                    ])->get($parm);
                $oVal['two_class_list'] = [];
                if (!empty($twoList)) {
                    $twoList = $twoList->toArray();
                    foreach ($twoList as &$tVal) {
                        $sList = ShopCategoryLogic::query()
                            ->where([
                                'category_status' => 1,
                                'status_delete' => 0,
                                ['category_level', '!=', null],
                                'category_parent_id' => $tVal['category_id']
                            ])->get($parm);
                        if (!empty($sList)) {
                            $tVal['three_class_list'] = $sList->toArray();
                        }
                    }
                    $oVal['two_class_list'] = $twoList;
                }
            }
        }

        return success($oneList);
    }

    /**
     * categoryByParentId
     * @param IdRequest $request
     * @return JsonResponse
     */
    public function categoryByParentId(IdRequest $request): JsonResponse
    {
        return success(DB::table('shop_category')
            ->where('category_status', 1)
            ->where('category_parent_id', $request->id)->orderBy('sort')
            ->pluck('category_name', 'category_id'));
    }

    /**
     * getProductCategory
     * @return JsonResponse
     */
    public function getProductCategory(): JsonResponse
    {
        $builder = DB::table('shop_category')->where('category_status', 1)->orderBy('sort');
        $id = (int)request()->post('id');
        if ($id > 0) $builder->where('category_top_parent_id', $id);
        return success($builder->get([
            'category_id',
            'category_name',
            'category_parent_id',
            'class_img_url',
            'class_jump_url'
        ]));
    }

    /**
     * getProductCategory
     * @return JsonResponse
     */
    public function goodsCategory(Request $request): JsonResponse
    {
        $list = ActivityZoneLogic::query()
            ->where('is_show', 1)
            ->orderByDesc('sort')
            ->orderByDesc('created_at')
            ->get()->toArray();

        foreach ($list as &$val) {
            $classList = ShopCategoryLogic::query()
                ->where([
                    'category_status' => 1,
                    'status_delete' => 0,
                    ['category_parent_id', '>', 0]
                ])->orderBy('sort')
                ->get(['category_id', 'category_name', 'class_img_url'])->toArray();
            $val['class_list'] = [];
            foreach ($classList as $key => $cVal) {
                if (ShopProductLogic::query()->where([
                    'product_status' => 2,
                    'product_is_delete' => 2,
                    'product_category_id_2' => $cVal['category_id'],
                    'product_activity_type' => $val['id']
                ])->exists()) {
                    $val['class_list'][] = $cVal;
                }
            }
        }

        return success($list);
    }

    /**
     * agent
     * @return JsonResponse
     */
    public function agent(): JsonResponse
    {
        return success(cache()->has('agent:' . userId()));
    }
}
